package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.entity.Admin;
import com.example.springboot.service.AdminService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Resource
    AdminService adminService;

    /*
     * 分页查询
     * @param pageNum:当前页码
     * @param pageSize:每页显示条数
     */
    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             Admin admin){
        PageInfo<Admin> adminPageInfo = adminService.selectPage(pageNum, pageSize, admin);
        return Result.success(adminPageInfo);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Admin admin)  //@RequestBody 接收前端传来的json数据
    {
        adminService.add(admin);
        return Result.success();//不需要返回数据，不用传递
    }

    @PutMapping("/update")
    public Result update(@RequestBody Admin admin)  //@RequestBody 接收前端传来的json数据
    {
        adminService.update(admin);
        return Result.success();//不需要返回数据，不用传递
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) // @PathVariable 接收路径中的参数
    {
        adminService.deleteById(id);
        return Result.success();
    }

    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Admin> adminList) // 传递的不只有一个对象，所以用List
    {
        adminService.deleteBatch(adminList);
        return Result.success();
    }

    @PostMapping("/updatePassword")
    public Result updatePassword(@RequestBody Admin admin){
        adminService.updatePassword(admin);
        return Result.success();
    }

}
